package com.aji.wineserver.controller;

import com.aji.allcommon.result.Result;
import com.aji.wineserver.domain.DataOrgin;
import com.aji.wineserver.domain.HistoryData;
import com.aji.wineserver.domain.MeasureMethods;
import com.aji.wineserver.domain.Time;
import com.aji.wineserver.domain.vo.WineMoreVo;
import com.aji.wineserver.service.DataOrginService;
import com.aji.wineserver.service.HistoryDataService;
import com.aji.wineserver.service.MeasureMethodsService;
import com.aji.wineserver.service.TimeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "酒类更多信息")
@RestController
@RequestMapping("/wineMore")
@Slf4j
@RequiredArgsConstructor
public class WineMoreController {
    private final HistoryDataService historyDataService;
    private final MeasureMethodsService measureMethodsService;
    private final TimeService timeService;
    private DataOrginService dataOrginService;
    @GetMapping("/{wineId}")
    public Result moreData(@PathVariable Integer wineId){
        List<WineMoreVo> wineMoreVoList = new ArrayList<>();
        //查询历史数据表
        LambdaQueryWrapper<HistoryData> historyDataLambdaQueryWrapper = new LambdaQueryWrapper<HistoryData>()
                .select()
                .eq(wineId!=null,HistoryData::getWineId,wineId);
        List<HistoryData> historyDataList = historyDataService.list(historyDataLambdaQueryWrapper);
        for (HistoryData h:historyDataList
             ) {
            //录入相同数据
            WineMoreVo wineMoreVo = WineMoreVo.builder().id(h.getId()).data(h.getData()).build();
            //查三个细节数据
            LambdaQueryWrapper<MeasureMethods> measureMethodsLambdaQueryWrapper = new LambdaQueryWrapper<MeasureMethods>()
                    .select()
                    .eq(MeasureMethods::getId,h.getMeasureMethodsId());
            MeasureMethods measureMethodsServiceOne = measureMethodsService.getOne(measureMethodsLambdaQueryWrapper);
            LambdaQueryWrapper<Time> timeLambdaQueryWrapper = new LambdaQueryWrapper<Time>()
                    .select()
                    .eq(Time::getId,h.getTimeId());
            Time timeServiceOne = timeService.getOne(timeLambdaQueryWrapper);
            LambdaQueryWrapper<DataOrgin>dataOrginLambdaQueryWrapper = new LambdaQueryWrapper<DataOrgin>()
                    .select()
                    .eq(DataOrgin::getId,h.getDataOrginId());
            DataOrgin dataOrginServiceOne = dataOrginService.getOne(dataOrginLambdaQueryWrapper);
            //插入三个细节数据
            wineMoreVo.setMeasureMethodsHead(measureMethodsServiceOne.getHead());
            wineMoreVo.setMeasureMethodsName(measureMethodsServiceOne.getName());
            wineMoreVo.setTime(timeServiceOne.getTime());
            wineMoreVo.setDataOrginName(dataOrginServiceOne.getName());
            //放入列表
            wineMoreVoList.add(wineMoreVo);
        }

        return Result.success(wineMoreVoList);
    }
}
